package org.example.config;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.example.model.messagequeue.Room;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author huangy
 * @description 房间消息消费者监听器
 * @since 2022/11/11 11:50
 **/
@Configuration
@Slf4j
public class RocketRoomConsumerListener implements MessageListenerConcurrently {
    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        messageList.forEach(msg -> {
            log.info("收到消息: 消息id " + msg.getMsgId());
            String messageBody = new String(msg.getBody(), StandardCharsets.UTF_8);
            Room room = JSON.parseObject(messageBody, Room.class);
            log.info("房间消息: " + room);
        });
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
